package com.hz.service;


import com.hz.dao.OrUserDao;
import com.hz.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserService {
    @Autowired
    private OrUserDao server;

    public User orLogin(User user) {
        User res = server.OrList(user);
        if (res != null && res.getState().equals("正常")) {
            if (user.getPassword().equals(res.getPassword())) {
                //账号存在，且密码相同，发放随机数作为本次登录的token
                res.setToken(UUID.randomUUID().toString());
                // 把token存到数据库
                server.setToken(res);
                User tok = server.getToken(res);
                return tok;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public User getListByToken(String token) {
        User res = server.getListByToken(token);
        if (res != null) {
            List<String> js = Arrays.asList("admin");
            res.setRoles(js);
            return res;
        } else {
            return null;
        }
    }

    public Map<String, Object>  listAdmin(User user) {
        //分页功能
        long le=(user.getPage()-1)*user.getLimit();
        user.setLen(le);
        User user1 = server.getListByToken(user.getToken());
        user.setId(user1.getId());
        int num = server.gettotal(user);
        List<User> res = server.getlistAdmin(user);
        Map<String, Object> res2 = new HashMap<String, Object>();
        res2.put("total", num);
        res2.put("items", res);
        if (res2 != null) {
            return res2;
        } else {
            return null;
        }
    }

    public int addAdmin(User user) {
        User ad = server.getAdminByUser(user);
        if (ad == null) {
            // 根据账户来查询token是否为空，是则允许添加
            User zh = server.OrList(user);
            System.out.println(zh);
            if (zh == null) {
                User user1 = server.getListByToken(user.getToken());
                user.setPid(user1.getId());
                int res = server.addAdmin(user);
                if (res > 0) {
                    return 1;
                } else {
                    return 0;
                }
            } else {
                return 2;
            }
        } else {
            return 2;
        }
    }

    public int updateAdmin(User user) {
        User com = server.getUserByid(user);
        //判断用户是否修改姓名、部门和账户是就进行验证 没有修改这三个直接让他修改
        if (!user.getName().equals(com.getName()) || !user.getPostName().equals(com.getPostName()) || !user.getUsername().equals(com.getUsername())) {
            //如果用户修改了姓名或者部门或者账户就进来重复判断
            //用户要修改的账户和部门是否已存在？存在则不能修改
            User ad = server.getAdminByUser(user);
            if (ad == null) {
                //如果不存在就去判断用户的账户是否修改了是则进行判断，否则就进行修改
                if (!user.getUsername().equals(com.getUsername())){
                    User zh = server.OrList(user);
                    if (zh == null) {
                        int res = server.updateAdmin(user);
                        if (res > 0) {
                            return res;
                        } else {
                            return 0;
                        }
                    }
                }else {
                    int res = server.updateAdmin(user);
                    if (res > 0) {
                        return res;
                    } else {
                        return 0;
                    }
                }
            }
            return 0;
        } else {
            int res = server.updateAdmin(user);
            if (res > 0) {
                return res;
            } else {
                return 0;
            }
        }

    }

    public int deleteAdmin(User user) {
        int res = server.deleteAdmin(user);
        if (res > 0) {
            return res;
        } else {
            return 0;
        }
    }


}
